﻿using System;

namespace Byleist.Caching
{
    public class ExpirationInvalidationPolicy : ICacheInvalidationPolicy
    {
        private readonly long expirationTimeTicks;
        private long lastCacheRenewal;

        public ExpirationInvalidationPolicy(TimeSpan expirationTimeSpan)
        {
            this.expirationTimeTicks = expirationTimeSpan.Ticks;
        }

        public void CacheRequested()
        {
        }

        public void InvalidateCache()
        {
            this.lastCacheRenewal = DateTime.MinValue.Ticks;
        }

        public bool IsCacheValid()
        {
            return (DateTime.Now.Ticks - this.lastCacheRenewal) < this.expirationTimeTicks;
        }

        public void CacheRenewed()
        {
            this.lastCacheRenewal = DateTime.Now.Ticks;
        }
    }
}